package com.fenghuajueli.libbasecoreui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.R;
import com.fenghuajueli.libbasecoreui.listener.OnBaseClick;

/**
 * 通用提示选择对话框
 */
public class CommonInputDialog extends Dialog {

    private EditText tvContent;
    private TextView tvCancel;
    private TextView tvConfirm;
    private String cancelName = "取消";
    private String confirmName = "确定";
    private String oldContent;

    private View.OnClickListener cancelListener;
    private OnBaseClick<String> confirmListener;


    public CommonInputDialog(@NonNull Context context) {
        super(context, R.style.StyleBaseDialog);
    }

    public CommonInputDialog(@NonNull Context context, String oldContent) {
        super(context, R.style.StyleBaseDialog);
        this.oldContent = oldContent;
    }


    public CommonInputDialog(@NonNull Context context, String cancelName, String confirmName) {
        super(context, R.style.StyleBaseDialog);
        this.cancelName = cancelName;
        this.confirmName = confirmName;
    }


    public void setCancelListener(View.OnClickListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    public void setConfirmListener(OnBaseClick<String> confirmListener) {
        this.confirmListener = confirmListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_common_input_layout);
        initView();
    }


    private void initView() {
        tvContent = findViewById(R.id.tvContent);
        tvCancel = findViewById(R.id.tvCancel);
        tvConfirm = findViewById(R.id.tvConfirm);
        tvCancel.setText(cancelName);
        tvConfirm.setText(confirmName);
        if (!TextUtils.isEmpty(oldContent)) {
            tvContent.setText(oldContent);
        }
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (cancelListener != null) {
                    cancelListener.onClick(v);
                }
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = tvContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.showShort("请先输入内容");
                    return;
                }
                dismiss();
                if (confirmListener != null) {
                    confirmListener.onClick(content);
                }
            }
        });
    }


}

